package org.rcloud.io.service.impl;

import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.mapper.FileMapper;
import org.rcloud.io.model.File;
import org.rcloud.io.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2019/2/26.
 */
@Service(value = "FileService")
public class FileServiceImpl implements FileService {
    @Autowired
    private FileMapper fileMapper;


    @Override
    public ResultVo save(File file) {

        if (file.equals("") || file == null) {
            return ResultVo.setError("传入数据为空");
        } else {
            try {
                int i = fileMapper.insertSelective(file);
                return ResultVo.setOK(i);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultVo.setError(e.getMessage());
            }
        }

    }

    @Override
    public PageVo<File> findFile() {
        try {
            List<File> list = fileMapper.selectAll();
            return PageVo.setOk(list);
        } catch (Exception e) {
            e.printStackTrace();
            return PageVo.setError(e.getMessage());
        }
    }

    @Override
    public PageVo<File> findFileByNum(String id) {

        try {
            List<File> list = fileMapper.selectByNum(id);
            return PageVo.setOk(list);
        } catch (Exception e) {
            e.printStackTrace();
            return PageVo.setError(e.getMessage());
        }

    }

    @Override
    public ResultVo updateFile(int id) {

        try {
            int i = fileMapper.updateById(id);
            return ResultVo.setOK(i);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.setError(e.getMessage());
        }
    }

    @Override
    public ResultVo findFileById(int id) {

        try {
            File file = fileMapper.selectByPrimaryKey(id);
            return ResultVo.setOK(file);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.setError(e.getMessage());
        }

    }

    @Override
    public ResultVo updateFlagAll(String eid) {

        try {
            int a = fileMapper.updateFlagAll(eid);
            return ResultVo.setOK(a);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.setError(e.getMessage());
        }

    }

    @Override
    public ResultVo updateFlag(int id) {

        try {
            int a = fileMapper.updateFlag(id);
            return ResultVo.setOK(a);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.setError(e.getMessage());
        }

    }

    @Override
    public PageVo<File> selectByFileEndor(File eid) {

        try {
            List<File> list = fileMapper.selectByFileEndor(eid);
            return PageVo.setOk(list);
        } catch (Exception e) {
            e.printStackTrace();
            return PageVo.setError(e.getMessage());
        }

    }
}